package com.controller;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.pojo.User;
import com.service.UserService;

@Controller
public class LogController {
	@Autowired
	UserService userService;
	//登录方法，只接受post方法
	@RequestMapping(value="/login",method=RequestMethod.POST)
	@ResponseBody
	public int login(User user,HttpServletRequest req) {
		int judge;
		User user1=userService.selectOne(user);
		if(user1==null) {
			user.setU_password(null);
			if(userService.selectOne(user)==null) {
				judge=-1;//账号错误返回-1
			}
			else {
				judge=0;//密码错误返回0
			}
		}else {
			//登录成功创建session
			HttpSession session=req.getSession();
			session.setAttribute("user", user1);
			judge=1;
		}
		
		return judge;
	}
	
	
	//注册方法，只接受post方法
	@RequestMapping(value="/logon",method=RequestMethod.POST)
	@ResponseBody
	public int logon(User user) {
		User user1=userService.selectOneAcc(user);
		if(user1==null) {
			return 0;//账号重复返回0
		}else {
			int a=(int) userService.insert(user);
			if(a==1) {
				return 1;//注册成功
			}else {
				return -1;//注册失败
			}
			
		}
		
	}
	//退出登录
	@RequestMapping("/logout")
	@ResponseBody
	public void logout(HttpServletRequest req) {
		req.getSession().removeAttribute("user");
	}
}
